package de.unibi.mmi.homeauto.team1.drafts;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import de.unibi.mmi.homeauto.team1.indicator.PageIndicator;
import de.unibi.mmi.homeauto.team1.mainview.MainViewActivity;

public abstract class DraftViewPager {

	protected View view;

	/**
	 * 
	 */
	protected MyAdapter adapter;
	protected ViewPager viewPager;

	protected PageIndicator pageIndicatorBottom;

	protected PageIndicator pageIndicatorTitle;

	protected DraftFragment[] fragments;

	protected int startItem = 0;

	protected MainViewActivity mva;

	protected void init(final MainViewActivity mva, int layoutID, int pagerID,
			int indicatorIDTitle, int indicatorIDBottom) {
		this.mva = mva;

		view = LayoutInflater.from(mva).inflate(layoutID, null);

		adapter = new MyAdapter(mva.getSupportFragmentManager());

		viewPager = (ViewPager) view.findViewById(pagerID);

		viewPager.setAdapter(adapter);

		viewPager.setCurrentItem(startItem + 1);

		pageIndicatorBottom = (PageIndicator) view
				.findViewById(indicatorIDBottom);
		pageIndicatorBottom.setViewPager(viewPager);
		pageIndicatorBottom.setCurrentItem(viewPager.getCurrentItem());

		pageIndicatorTitle = (PageIndicator) view
				.findViewById(indicatorIDTitle);
		pageIndicatorTitle.setViewPager(viewPager);
		pageIndicatorTitle.setCurrentItem(viewPager.getCurrentItem());

		pageIndicatorTitle.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

				if (arg0 == fragments.length + 1) {
					viewPager.setCurrentItem(1, true);
				} else if (arg0 == 0) {
					viewPager.setCurrentItem(fragments.length, true);
				}

				pageIndicatorBottom.setCurrentItem(viewPager.getCurrentItem());
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	public int getCurrentItem() {
		return this.viewPager.getCurrentItem() - 1;
	}

	public void setCurrentItem(int pos) {
		this.viewPager.setCurrentItem(pos + 1, true);
	}

	public void setToStartitem() {
		setCurrentItem(this.startItem);
	}

	public View getView() {
		return view;
	}

	private class MyAdapter extends FragmentPagerAdapter {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public final String getPageTitle(final int position) {
			if (position == 0) {
				// return fragments[fragments.length - 1].getTitle();
				return "";
			}
			if (position == fragments.length + 1) {
				// return fragments[0].getTitle();
				return "";
			}
			return fragments[position - 1].getTitle();
		}

		@Override
		public int getCount() {
			return fragments.length + 2;
		}

		@Override
		public Fragment getItem(int position) {
			if (position == 0 || position == fragments.length + 1) {
				return new Fragment();
			}

			return fragments[position - 1];
		}
	}

	public abstract boolean onBackKey();

	public void serverValuesUpdated() {
		for (DraftFragment frag : fragments) {
			try {
				frag.updateFromServer();
			} catch (Exception e) {
				mva.debug(this, frag.getClass().getSimpleName() + " Error: "
						+ e.getMessage());
			}
		}
	}
}
